<?php


namespace App\Repositories;

//导出数据从数据库获取数据的方法不同方法
use App\Services\Tools\Export;
use Illuminate\Support\Facades\DB;
use App\Services\Tools\ObsService;
use Illuminate\Support\Facades\Log;
use App\Services\Tools\HelperService;
use Illuminate\Support\Facades\Storage;
use App\Enums\ErmEnum;

class GetDataToExcelRepository
{

    private $maxExcelLine = 5000000;                                                                  //最大导出数量
    private $oneExcelLine = 50000;                                                                    //一次写入数据量

    /**
     * 压缩文件夹中的所有文件
     *
     * @param  string  $dir  要压缩的目录，绝对路径
     * @param  string  $zipFilename  压缩文件的名称，绝对路径
     * @return bool 是否压缩成功
     */
    private function createZipFile($path, $zipFilename)
    {
        $flag = false;
        $zip = new \ZipArchive;
        if ($zip->open($zipFilename, \ZipArchive::CREATE | \ZIPARCHIVE::OVERWRITE) === true) {
            $handle = opendir($path);
            while ($file = readdir($handle)) {
                if (is_file($path . '/' . $file)) {
                    $flag = true;
                    $zip->addFile($path . '/' . $file, $file);
                }
            }
            closedir($handle);

            if ($zip->close()) {
                return $flag;
            } else {
                return false;
            }
        }
    }


    /**
     * 删除目录及里面的文件
     *
     * @param  string  $dirName  目录完整路径
     */
    public function rmDir($path)
    {
        if (!file_exists($path)) {
            return true;
        }

        if ($handle = @opendir($path)) {
            while (($file = readdir($handle)) !== false) {
                if (($file == ".") || ($file == "..")) {
                    continue;
                }
                if (is_dir($path . '/' . $file)) {
                    // 递归
                    $this->rmDir($path . '/' . $file);
                } else {
                    unlink($path . '/' . $file); // 删除文件
                }
            }
            @closedir($handle);
            rmdir($path);
        }
    }


    /** 上传文件到OSS
     *
     * @param $fileUrl 上传文件源地址
     * @param $fileName OSS上的文件名称
     * @return string oss文件地址
     */
    private function upOssAndDelLocal($fileUrl, $fileName)
    {
        $Osspath = '/qipao/admin/export/' . date('Ymd-His', time()) . '_' . rand(1000, 9999) . '_' . $fileName;//分批次导出excel文件的临时文件夹存放地址
        $uploadRes = ObsService::putObs($Osspath, $fileUrl);
        $url = env('OBS_HOST') . '/' . $Osspath;

        return $url;
    }

    //  分批次获取所有用户背包礼物
    public function getUserBackpackGift($whereArr)
    {
        $i = 0;
        $offset = 0;
        $tempDirName = storage_path('excel/' . date('Ymd-His', time()) . '_' . rand(1000, 9999) . '/'); //分批次导出excel文件的临时文件夹存放地址
        $zipName = 'user_backpack_gift';

        $giftBackpackModel = app('model_gift_backpack');
        $userDataModel = app('model_user_data');
        $giftModel = app('model_gift');
        while ($i < $this->maxExcelLine) { //在循环中分批次获取数据

            $result = app('model_gift_backpack')
                ->from($giftBackpackModel->getTable() . " as t1")
                ->select([
                    "t1.*",
                    "t2.nickname",
                    "t2.user_code",
                    "t3.name",
                    "t3.price",
                ])
                ->leftJoin(DB::raw($userDataModel->getTable() . " as t2"), "t1.user_id", "=", "t2.user_id")
                ->leftJoin(DB::raw($giftModel->getTable() . " as t3"), "t1.gift_id", "=", "t3.id")
                ->where($whereArr)->orderBy('t1.id', 'desc')->offset($offset)->limit($this->oneExcelLine)->get();
            $result = $result->toArray();

            $fields = ['序号', '用户id', '用户昵称', '礼物名称', '礼物价格（金币）', '礼物数量'];
            $res = [];
            foreach ($result as $key => $value) {
                $res[$key][0] = $value['id'];
                $res[$key][1] = $value['user_code'];
                $res[$key][2] = $value['nickname'];
                $res[$key][3] = $value['name'];
                $res[$key][4] = $value['price'];
                $res[$key][5] = "\t" . $value['number'];
            }
            $i += $this->oneExcelLine;                //分批次读取
            $order = intval($i / $this->oneExcelLine); //分批次导出表格的第几张
            $offset = $order * $this->oneExcelLine;
            try {
                $export = new Export();
                $export->writeCsv($tempDirName, $zipName . '-' . $order, $fields, $res);
                unset($export);
            } catch (\Exception $exception) {              //有一次写入失败的情况
                $this->rmDir($tempDirName);                //删除前几轮写入成功的文件

                return false;
            }
            if (count($result) < $this->oneExcelLine) { //最后一轮导出
                break;
            }
            unset($result);
            unset($res);
        }

        $saveLocalPath = $tempDirName . '/' . $zipName . '.zip';
        //压缩存放xls文件的目录 打包文件
        $res = $this->createZipFile($tempDirName, $saveLocalPath);
        if (!$res) { //压缩失败
            $this->rmDir($tempDirName);

            return false;
        }
        $OssPath = $uploadRes = $this->upOssAndDelLocal($saveLocalPath, $zipName . '.zip');
        $this->rmDir($tempDirName);

        return $OssPath;
    }

    //收支日志 分批次导出
    public function getAccountLog($whereArr)
    {
        $tempDirName = storage_path('excel/' . date('Ymd-His', time()) . '_' . rand(1000, 9999) . '/'); //分批次导出excel文件的临时文件夹存放地址
        $type_list = app('repo_account_log')->typeList();
        $type_list_arr = [];
        foreach ($type_list as $val) {
            $type_list_arr[$val['change_type']] = $val['name'];
        }

        if ($whereArr['account_type'] == 3) {
            return $this->getAllAccountLog($whereArr);
        }

        if ($whereArr['account_type'] == 1) {
            $tableName = 'ln_account_money_log';
        } else {
            $tableName = 'ln_account_earnings_log';
        }

        $where = [];
        if (isset($whereArr['mobile'])) {
            $where[] = ['u.mobile', '=', $whereArr['mobile']];
        }
        if (isset($whereArr['nickname'])) {
            $where[] = ['ud.nickname', 'like', '%' . $whereArr['nickname'] . '%'];
        }
        if (isset($whereArr['user_code'])) {
            $where[] = ['ud.user_code', '=', $whereArr['user_code']];
        }
        if (isset($whereArr['change_type'])) {
            $where[] = ['al.change_type', '=', $whereArr['change_type']];
        }
        if (isset($whereArr['happen_time_start'])) {
            //$where[] = ['al.created_at', '>=', strtotime($whereArr['happen_time_start'])];
            $where[] = ['al.created_at', '>=', $whereArr['happen_time_start']];
        }

        if (isset($whereArr['happen_time_end'])) {
            //$where[] = ['al.created_at', '<=', strtotime($whereArr['happen_time_end'])];
            $where[] = ['al.created_at', '<=', $whereArr['happen_time_end']];
        }
        $model = DB::table($tableName . ' as al')
            ->leftJoin('ln_user as u', 'u.user_id', '=', 'al.user_id')
            ->leftJoin('ln_user_data as ud', 'ud.user_id', '=', 'al.user_id')
            ->select(['al.id', 'al.user_id', 'al.change_money', 'al.change_type', 'al.change_desc', 'al.created_at', 'u.mobile', 'ud.nickname', 'ud.user_code']);
        if (!empty($where)) {
            $model = $model->where($where);
        }
        $i = 0;
        $offset = 0;
        $zipName = 'user_account';

        while ($i < $this->maxExcelLine) { //在循环中分批次获取数据
            $result = $model->offset($offset)->limit($this->oneExcelLine)->get();
            $result = json_decode(json_encode($result), true);
            $i += $this->oneExcelLine;                //分批次读取
            $res = [];
            foreach ($result as $key => $value) {
                $res[$key][0] = $value['id'];
                $res[$key][1] = $value['user_code'];
                $res[$key][2] = $value['nickname'];
                $res[$key][3] = "\t" . $value['mobile'];
                $res[$key][4] = $value['change_money'];
                $res[$key][5] = $type_list_arr[$value['change_type']];
                $res[$key][6] = $value['change_desc'];
                $res[$key][7] = "\t" . $value['created_at'];
            }
            $fields = ['序号', '用户编号', '用户昵称', '手机号', '金币数量', '类别', '备注', '时间'];
            $order = intval($i / $this->oneExcelLine); //分批次导出表格的第几张
            $offset = $order * $this->oneExcelLine;
            try {
                $export = new Export();
                $export->writeCsv($tempDirName, $zipName . '-' . $order, $fields, $res);
                unset($export);
            } catch (\Exception $exception) {              //有一次写入失败的情况
                $this->rmDir($tempDirName);                //删除前几轮写入成功的文件

                return false;
            }
            if (count($result) < $this->oneExcelLine) { //最后一轮导出
                break;
            }
            unset($result);
            unset($res);
        }
        $saveLocalPath = $tempDirName . '/' . $zipName . '.zip';
        //压缩存放xls文件的目录 打包文件
        $res = $this->createZipFile($tempDirName, $saveLocalPath);
        if (!$res) { //压缩失败
            $this->rmDir($tempDirName);

            return false;
        }
        $OssPath = $uploadRes = $this->upOssAndDelLocal($saveLocalPath, $zipName . '.zip');
        $this->rmDir($tempDirName);

        return $OssPath;
    }
    //全部收支日志 分批次导出
    public function getAllAccountLog($whereArr)
    {
        $tempDirName = storage_path('excel/' . date('Ymd-His', time()) . '_' . rand(1000, 9999) . '/'); //分批次导出excel文件的临时文件夹存放地址
        $type_list = app('repo_account_log')->typeList();
        $type_list_arr = [];
        foreach ($type_list as $val) {
            $type_list_arr[$val['change_type']] = $val['name'];
        }


        $i = 0;
        $offset = 0;
        $zipName = 'user_account';
        $whereArr['perPage'] = $this->oneExcelLine;

        $export_page_info = [];
        while ($i < $this->maxExcelLine) { //在循环中分批次获取数据
            $result = (new AccountLogRepository())->getAllUserAccountLog($whereArr, $export_page_info);
            $export_page_info = $result['page_info_arr'];
            $result = $result['data'];


            $i += $this->oneExcelLine;                //分批次读取
            $res = [];
            foreach ($result as $key => $value) {
                $res[$key][0] = $value['id'];
                $res[$key][1] = $value['user_code'];
                $res[$key][2] = $value['nickname'];
                $res[$key][3] = "\t" . $value['mobile'];
                $res[$key][4] = $value['change_money'];
                $res[$key][5] = $type_list_arr[$value['change_type']];
                $res[$key][6] = $value['change_desc'];
                $res[$key][7] = "\t" . $value['created_at'];
            }
            $fields = ['序号', '用户编号', '用户昵称', '手机号', '金币数量', '类别', '备注', '时间'];
            $order = intval($i / $this->oneExcelLine); //分批次导出表格的第几张
            $offset = $order * $this->oneExcelLine;
            try {
                $export = new Export();
                $export->writeCsv($tempDirName, $zipName . '-' . $order, $fields, $res);
                unset($export);
            } catch (\Exception $exception) {              //有一次写入失败的情况
                $this->rmDir($tempDirName);                //删除前几轮写入成功的文件

                return false;
            }
            if (count($result) < $this->oneExcelLine) { //最后一轮导出
                break;
            }
            unset($result);
            unset($res);
        }
        $saveLocalPath = $tempDirName . '/' . $zipName . '.zip';
        //压缩存放xls文件的目录 打包文件
        $res = $this->createZipFile($tempDirName, $saveLocalPath);
        if (!$res) { //压缩失败
            $this->rmDir($tempDirName);

            return false;
        }
        $OssPath = $uploadRes = $this->upOssAndDelLocal($saveLocalPath, $zipName . '.zip');
        $this->rmDir($tempDirName);

        return $OssPath;
    }
    //管理充值纪录异步导出
    public function adminRechargeQuery($whereArr)
    {
        $tempDirName = storage_path('excel/' . date('Ymd-His', time()) . '_' . rand(1000, 9999) . '/'); //分批次导出excel文件的临时文件夹存放地址
        $i = 0;
        $zipName = 'admin_recharge';
        $whereArr['page'] = 1;
        $whereArr['perPage'] = $this->oneExcelLine;
        while ($i < $this->maxExcelLine) { //在循环中分批次获取数据
            $result = app('repo_admin_recharge')->getAdminRechargeQueryList($whereArr);
            $fields = [
                '用户昵称',
                '用户编号',
                '充值金额',
                '充值类型',
                '管理员名称',
                '时间',
                '充值目的',
                '备注',
            ];
            $res = [];
            foreach ($result as $key => $value) {
                $res[$key][0] = "\t" . HelperService::wxNickNameFormat($value->nickname);
                $res[$key][1] = $value->user_id;
                $res[$key][2] = $value->gold;
                $res[$key][3] = $value->type_name;
                $res[$key][4] = $value->admin_name;
                $res[$key][5] = "\t" . $value->add_time;
                $res[$key][6] = $value->purpose_desc;
                $res[$key][7] = $value->remark;
            }

            $i += $this->oneExcelLine;                //分批次读取
            try {
                $export = new Export();
                $export->writeCsv($tempDirName, $zipName . '-' . $whereArr['page'], $fields, $res);
                unset($export);
            } catch (\Exception $exception) {              //有一次写入失败的情况
                $this->rmDir($tempDirName);                //删除前几轮写入成功的文件

                return false;
            }
            $whereArr['page']++;
            if (count($res) < $this->oneExcelLine) { //最后一轮导出
                break;
            }
            unset($result);
            unset($res);
        }
        $saveLocalPath = $tempDirName . '/' . $zipName . '.zip';
        //压缩存放xls文件的目录 打包文件
        $res = $this->createZipFile($tempDirName, $saveLocalPath);
        if (!$res) { //压缩失败
            $this->rmDir($tempDirName);

            return false;
        }
        $OssPath = $uploadRes = $this->upOssAndDelLocal($saveLocalPath, $zipName . '.zip');
        $this->rmDir($tempDirName);

        return $OssPath;
    }

    //支付宝提现
    public function aliWithDrawQueryAsync(array $where)
    {
        $tempDirName = storage_path('excel/' . date('Ymd-His', time()) . '_' . rand(1000, 9999) . '/'); //分批次导出excel文件的临时文件夹存放地址
        $i = 0;
        $zipName = 'user_aili_withdraw';
        $where['page'] = 1;
        $where['perPage'] = $this->oneExcelLine;
        while ($i < $this->maxExcelLine) { //在循环中分批次获取数据
            $result = app('repo_user_withdraw')->getAliWithDrawQueryList($where);
            $fields = [
                '序号（必填）',
                '收款方支付宝账号（必填）',
                '收款方姓名（必填）',
                '金额（必填，单位：元）',
                '备注（选填）',
                '申请时间',
                '审批时间',
                '用户编号',
                '手机号',
                '身份证号',
            ];
            $res = [];
            foreach ($result as $key => $value) {
                $res[$key][0] = $value->id;
                $res[$key][1] = $value->bank_num;
                $res[$key][2] = $value->cardholder;
                $res[$key][3] = $value->money;
                $res[$key][4] = $value->remark;
                $res[$key][5] = "\t" . $value->add_time;
                $res[$key][6] = "\t" . $value->audit_time;
                $res[$key][7] = $value->user_code;
                $res[$key][8] = "\t" . $value->tmobile . ' ';
                $res[$key][9] = "\t" . $value->id_number;
            }
            $i += $this->oneExcelLine;                //分批次读取
            try {
                $export = new Export();
                $export->writeCsv($tempDirName, $zipName . '-' . $where['page'], $fields, $res);
                unset($export);
            } catch (\Exception $exception) {              //有一次写入失败的情况
                $this->rmDir($tempDirName);                //删除前几轮写入成功的文件

                return false;
            }
            $where['page']++;
            if (count($res) < $this->oneExcelLine) { //最后一轮导出
                break;
            }
            unset($result);
            unset($res);
        }
        $saveLocalPath = $tempDirName . '/' . $zipName . '.zip';
        //压缩存放xls文件的目录 打包文件
        $res = $this->createZipFile($tempDirName, $saveLocalPath);
        if (!$res) { //压缩失败
            $this->rmDir($tempDirName);

            return false;
        }
        $OssPath = $uploadRes = $this->upOssAndDelLocal($saveLocalPath, $zipName . '.zip');
        $this->rmDir($tempDirName);
        return $OssPath;
    }


    //大众名单列表
    public function ermPublicListQueryAsync(array $where)
    {
        $tempDirName = storage_path('excel/' . date('Ymd-His', time()) . '_' . rand(1000, 9999) . '/'); //分批次导出excel文件的临时文件夹存放地址
        $i = 0;
        $zipName = 'erm_public_list';
        $where['page'] = 1;
        $where['perPage'] = $this->oneExcelLine;
        $type = ErmEnum::PUBLIC_TYPE;
        $page = $where['page'];
        $perPage = $where['perPage'];
        while ($i < $this->maxExcelLine) { //在循环中分批次获取数据
            $result = app('repo_erm_public_list')->getPublicList($where, $page, $perPage, $type);
            $fields = [
                '用户ID',
                '用户昵称',
                '用户类型',
                '注册时间',
                '注册天数',
                '累计充值订单',
                '累计充值金额',
                '最后充值时间',
                '白名单次数',
                '灰名单次数',
                '黑名单次数',
            ];
            $res = [];
            foreach ($result as $key => $value) {
                $res[$key][0] = $value->user_code;
                $res[$key][1] = "\t" . HelperService::wxNickNameFormat($value->nickname);
                $res[$key][2] = $value->user_type;
                $res[$key][3] = "\t" . $value->register_time;
                $res[$key][4] = $value->register_date;
                $res[$key][5] = $value->recharge_num;
                $res[$key][6] = $value->recharge_money;
                $res[$key][7] = "\t" . $value->last_recharge_time;
                $res[$key][8] = $value->white_num;
                $res[$key][9] = $value->gray_num;
                $res[$key][10] = $value->black_num;
            }
            $i += $this->oneExcelLine;                //分批次读取
            try {
                $export = new Export();
                $export->writeCsv($tempDirName, $zipName . '-' . $where['page'], $fields, $res);
                unset($export);
            } catch (\Exception $exception) {              //有一次写入失败的情况
                $this->rmDir($tempDirName);                //删除前几轮写入成功的文件

                return false;
            }
            $where['page']++;
            if (count($res) < $this->oneExcelLine) { //最后一轮导出
                break;
            }
            unset($result);
            unset($res);
        }
        $saveLocalPath = $tempDirName . '/' . $zipName . '.zip';
        //压缩存放xls文件的目录 打包文件
        $res = $this->createZipFile($tempDirName, $saveLocalPath);
        if (!$res) { //压缩失败
            $this->rmDir($tempDirName);

            return false;
        }
        $OssPath = $uploadRes = $this->upOssAndDelLocal($saveLocalPath, $zipName . '.zip');
        $this->rmDir($tempDirName);
        return $OssPath;
    }

    /**
     * @Desc:飞屋盈利用户列表
     * @param $where
     * @return false|string
     * @author: shiquan
     * @Time: 2023/3/15 11:35
     */
    public function profitUserListExportLow($where)
    {
        $tempDirName = storage_path('excel/' . date('Ymd-His', time()) . '_' . rand(1000, 9999) . '/'); //分批次导出excel文件的临时文件夹存放地址
        $i = 0;
        $zipName = 'flying_profit_user_list';
        $where['user_code'] = $where['user_code'] ?? -1;
        $where['start_time'] = $where['start_time'] ?? '';
        $where['end_time'] = $where['end_time'] ?? '';
        $where['page'] = 1;
        $where['page_size'] = $this->oneExcelLine;
        $where['type'] = 1;
        while ($i < $this->maxExcelLine) {//在循环中分批次获取数据
            $result = app('repo_flying')->userList($where);
            $result = $result['list']['data'] ?? [];
            $fields = [
                '序号',
                '用户编号',
                '用户昵称',
                '用户投入',
                '用户产出',
                '盈利',
                '最后寻宝时间',
            ];
            $res = [];
            foreach ($result as $key => $value) {
                $res[$key][0] = $value['id'];
                $res[$key][1] = $value['user_code'];
                $res[$key][2] = "\t" . HelperService::wxNickNameFormat($value['nickname']);
                $res[$key][3] = $value['incoin_total'];
                $res[$key][4] = $value['outcoin_total'];
                $res[$key][5] = $value['sum_profit_loss_total'];
                $res[$key][6] = "\t" .$value['last_drawn_time'];
            }
            $i += $this->oneExcelLine;                //分批次读取
            try {
                $export = new Export();
                $export->writeCsv($tempDirName, $zipName . '-' . $where['page'], $fields, $res);
                unset($export);
            } catch (\Exception $exception) {              //有一次写入失败的情况
                $this->rmDir($tempDirName);                //删除前几轮写入成功的文件

                return false;
            }
            $where['profit_page']++;
            if (count($res) < $this->oneExcelLine) {//最后一轮导出
                break;
            }
            unset($result);
            unset($res);
        }
        $saveLocalPath = $tempDirName . '/' . $zipName . '.zip';
        //压缩存放xls文件的目录 打包文件
        $res = $this->createZipFile($tempDirName, $saveLocalPath);
        if (!$res) {//压缩失败
            $this->rmDir($tempDirName);

            return false;
        }
        $OssPath = $uploadRes = $this->upOssAndDelLocal($saveLocalPath, $zipName . '.zip');
        $this->rmDir($tempDirName);

        return $OssPath;
    }

    /**
     * @Desc:飞屋亏损用户列表
     * @param $where
     * @return false|string
     * @author: shiquan
     * @Time: 2023/3/15 11:35
     */
    public function lossUserListExportLow ($where)
    {
        $tempDirName = storage_path('excel/' . date('Ymd-His', time()) . '_' . rand(1000, 9999) . '/'); //分批次导出excel文件的临时文件夹存放地址
        $i = 0;
        $zipName = 'flying_loss_user_list';
        $where['user_code'] = $where['user_code'] ?? -1;
        $where['start_time'] = $where['start_time'] ?? '';
        $where['end_time'] = $where['end_time'] ?? '';
        $where['page'] = 1;
        $where['page_size'] = $this->oneExcelLine;
        $where['type'] = 2;
        while ($i < $this->maxExcelLine) {//在循环中分批次获取数据
            $result = app('repo_flying')->userList($where);
            $result = $result['list']['data'] ?? [];
            $fields = [
                '序号',
                '用户编号',
                '用户昵称',
                '用户投入',
                '用户产出',
                '盈利',
                '最后寻宝时间',
            ];
            $res = [];
            foreach ($result as $key => $value) {
                $res[$key][0] = $value['id'];
                $res[$key][1] = $value['user_code'];
                $res[$key][2] = "\t" . HelperService::wxNickNameFormat($value['nickname']);
                $res[$key][3] = $value['incoin_total'];
                $res[$key][4] = $value['outcoin_total'];
                $res[$key][5] = $value['sum_profit_loss_total'];
                $res[$key][6] = "\t" . $value['last_drawn_time'];
            }
            $i += $this->oneExcelLine;                //分批次读取
            try {
                $export = new Export();
                $export->writeCsv($tempDirName, $zipName . '-' . $where['page'], $fields, $res);
                unset($export);
            } catch (\Exception $exception) {              //有一次写入失败的情况
                $this->rmDir($tempDirName);                //删除前几轮写入成功的文件

                return false;
            }
            $where['loss_page']++;
            if (count($res) < $this->oneExcelLine) {//最后一轮导出
                break;
            }
            unset($result);
            unset($res);
        }
        $saveLocalPath = $tempDirName . '/' . $zipName . '.zip';
        //压缩存放xls文件的目录 打包文件
        $res = $this->createZipFile($tempDirName, $saveLocalPath);
        if (!$res) {//压缩失败
            $this->rmDir($tempDirName);

            return false;
        }
        $OssPath = $uploadRes = $this->upOssAndDelLocal($saveLocalPath, $zipName . '.zip');
        $this->rmDir($tempDirName);

        return $OssPath;
    }


    /**
     * @Desc:欢乐大作战盈利用户列表
     * @param $where
     * @return false|string
     * @author: shiquan
     * @Time: 2023/3/15 11:35
     */
    public function happyUserListExportProfit($where)
    {
        $tempDirName = storage_path('excel/' . date('Ymd-His', time()) . '_' . rand(1000, 9999) . '/'); //分批次导出excel文件的临时文件夹存放地址
        $i = 0;
        $zipName = 'happy_profit_user_list';
        $where['user_code'] = $where['user_code'] ?? -1;
        $where['start_time'] = $where['start_time'] ?? '';
        $where['end_time'] = $where['end_time'] ?? '';
        $where['page'] = 1;
        $where['page_size'] = $this->oneExcelLine;
        $where['type'] = 1;
        while ($i < $this->maxExcelLine) {//在循环中分批次获取数据
            $result = app('repo_happy')->userList($where);
            $result = $result['list']['data'] ?? [];
            $fields = [
                '序号',
                '用户编号',
                '用户昵称',
                '用户投入',
                '用户产出',
                '盈利',
                '最后寻宝时间',
            ];
            $res = [];
            foreach ($result as $key => $value) {
                $res[$key][0] = $value['id'];
                $res[$key][1] = $value['user_code'];
                $res[$key][2] = "\t" . HelperService::wxNickNameFormat($value['nickname']);
                $res[$key][3] = $value['incoin_total'];
                $res[$key][4] = $value['outcoin_total'];
                $res[$key][5] = $value['sum_profit_loss_total'];
                $res[$key][6] = "\t" .$value['last_drawn_time'];
            }
            $i += $this->oneExcelLine;                //分批次读取
            try {
                $export = new Export();
                $export->writeCsv($tempDirName, $zipName . '-' . $where['page'], $fields, $res);
                unset($export);
            } catch (\Exception $exception) {              //有一次写入失败的情况
                $this->rmDir($tempDirName);                //删除前几轮写入成功的文件

                return false;
            }
            $where['profit_page']++;
            if (count($res) < $this->oneExcelLine) {//最后一轮导出
                break;
            }
            unset($result);
            unset($res);
        }
        $saveLocalPath = $tempDirName . '/' . $zipName . '.zip';
        //压缩存放xls文件的目录 打包文件
        $res = $this->createZipFile($tempDirName, $saveLocalPath);
        if (!$res) {//压缩失败
            $this->rmDir($tempDirName);

            return false;
        }
        $OssPath = $uploadRes = $this->upOssAndDelLocal($saveLocalPath, $zipName . '.zip');
        $this->rmDir($tempDirName);

        return $OssPath;
    }

    /**
     * @Desc:欢乐大作战亏损用户列表
     * @param $where
     * @return false|string
     * @author: shiquan
     * @Time: 2023/3/15 11:35
     */
    public function happyUserListExportLoss($where)
    {
        $tempDirName = storage_path('excel/' . date('Ymd-His', time()) . '_' . rand(1000, 9999) . '/'); //分批次导出excel文件的临时文件夹存放地址
        $i = 0;
        $zipName = 'flying_loss_user_list';
        $where['user_code'] = $where['user_code'] ?? -1;
        $where['start_time'] = $where['start_time'] ?? '';
        $where['end_time'] = $where['end_time'] ?? '';
        $where['page'] = 1;
        $where['page_size'] = $this->oneExcelLine;
        $where['type'] = 2;
        while ($i < $this->maxExcelLine) {//在循环中分批次获取数据
            $result = app('repo_happy')->userList($where);
            $result = $result['list']['data'] ?? [];
            $fields = [
                '序号',
                '用户编号',
                '用户昵称',
                '用户投入',
                '用户产出',
                '盈利',
                '最后寻宝时间',
            ];
            $res = [];
            foreach ($result as $key => $value) {
                $res[$key][0] = $value['id'];
                $res[$key][1] = $value['user_code'];
                $res[$key][2] = "\t" . HelperService::wxNickNameFormat($value['nickname']);
                $res[$key][3] = $value['incoin_total'];
                $res[$key][4] = $value['outcoin_total'];
                $res[$key][5] = $value['sum_profit_loss_total'];
                $res[$key][6] = "\t" . $value['last_drawn_time'];
            }
            $i += $this->oneExcelLine;                //分批次读取
            try {
                $export = new Export();
                $export->writeCsv($tempDirName, $zipName . '-' . $where['page'], $fields, $res);
                unset($export);
            } catch (\Exception $exception) {              //有一次写入失败的情况
                $this->rmDir($tempDirName);                //删除前几轮写入成功的文件

                return false;
            }
            $where['loss_page']++;
            if (count($res) < $this->oneExcelLine) {//最后一轮导出
                break;
            }
            unset($result);
            unset($res);
        }
        $saveLocalPath = $tempDirName . '/' . $zipName . '.zip';
        //压缩存放xls文件的目录 打包文件
        $res = $this->createZipFile($tempDirName, $saveLocalPath);
        if (!$res) {//压缩失败
            $this->rmDir($tempDirName);

            return false;
        }
        $OssPath = $uploadRes = $this->upOssAndDelLocal($saveLocalPath, $zipName . '.zip');
        $this->rmDir($tempDirName);

        return $OssPath;
    }
}
